package com.chenfwind.serviceImpl;



import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chenfwind.IDao.BookMapper;
import com.chenfwind.IDao.OwnerMapper;
import com.chenfwind.IDao.TrajectoryMapper;
import com.chenfwind.common.StringUtils;
import com.chenfwind.domain.Book;
import com.chenfwind.domain.Owner;
import com.chenfwind.domain.Trajectory;
import com.chenfwind.domain.vo.BookVO;
import com.chenfwind.domain.vo.UserVO;
import com.chenfwind.service.BookService;

@Service("bookService")
public class BookServiceImpl implements BookService{
	
	@Autowired
	private BookMapper bookDao;
	
	@Autowired
	private OwnerMapper ownerDao;
	
	@Autowired
	private TrajectoryMapper trajectoryMapper;
	
	@Transactional
	public void newBook(Book book,String userId,BookVO bookVO){
		Owner owner = new Owner();
		owner.setBookId(book.getId());
		owner.setUserId(userId);
		owner.setId(StringUtils.genUUID());
		owner.setUpdateTiem(new Date());
		owner.setStatus(1);
		ownerDao.insertSelective(owner);
		
		Trajectory trajectory = new Trajectory();
		trajectory.setId(StringUtils.genUUID());
		trajectory.setBookId(book.getId());
		trajectory.setUserId(userId);
		trajectory.setX(Double.valueOf(bookVO.getX()));
		trajectory.setY(Double.valueOf(bookVO.getY()));
		trajectory.setRank(1);
		trajectory.setStatus(1);
		trajectory.setBooktrackname(bookVO.getTrackName());
		trajectoryMapper.insertSelective(trajectory);
		
		bookDao.insertSelective(book);
	}
	
	public List<Book> getAllBookByUser(String userId){
		List<Book> bookList = new ArrayList<Book>();
		List<Owner> ownerList = ownerDao.selectByUserID(userId,1);
		for (Owner owner : ownerList) { 
			bookList.add(bookDao.selectByPrimaryKey(owner.getBookId()));
		}
		return bookList;
	};
	
	public List<Book> getOtherBook(UserVO userVO){
		List<Owner> ownerList = ownerDao.selectByNoUserID(userVO.getId(),1); 
		Trajectory trajectory = null;
		Book book = null;
		List<Book> bookList = new ArrayList<Book>();
		for (Owner owner : ownerList) {
			trajectory = trajectoryMapper.selectByBookIDAndRank(owner.getBookId());
			if (trajectory != null){
				Double XX = Math.pow((Double.valueOf(trajectory.getX()) - Double.valueOf(userVO.getX())), 2);
				Double YY = Math.pow((Double.valueOf(trajectory.getY()) - Double.valueOf(userVO.getY())), 2);
				if (Math.sqrt(XX + YY) < 10000 )  {
					book = bookDao.selectByPrimaryKey(owner.getBookId());
					int dis = (int) Math.sqrt(XX + YY);
					book.setGategoryId(String.valueOf(dis));
					bookList.add(book);
				}
			}
		}
		return bookList;
	}
	
	public List<Book> getBookBy(String bookName, String category) {
		return bookDao.selectBookBy(bookName,category);
	}
	
	public String getBookNameById(String bookId){
		return bookDao.selectByPrimaryKey(bookId).getName();
	}
	
	public List<Book> getAllBook(){
		return bookDao.selectBook();
	}
	
	
	public int delBook(String bookId){
		return bookDao.updateDel(bookId);
	}
	/*
	 * 通过id获取书籍
	 */
	public Book findBookById(String id){
		return (Book)bookDao.selectByPrimaryKey(id);
	}
	
	/*
	 * 更新书籍
	 */
	public void updateBook(Book book){
		bookDao.updateByPrimaryKeySelective(book);
	}

	public BookMapper getBookDao() {
		return bookDao;
	}

	public void setBookDao(BookMapper bookDao) {
		this.bookDao = bookDao;
	}

	public OwnerMapper getOwnerDao() {
		return ownerDao;
	}

	public void setOwnerDao(OwnerMapper ownerDao) {
		this.ownerDao = ownerDao;
	}

	
	
}